<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class Wallet extends \app\BaseModel
{
    protected $name = "massage_service_wallet_list";
    protected $append = ["service_cash"];
    public function getServiceCashAttr($value, $data)
    {
        if(isset($data["apply_cash"]) && isset($data["true_cash"]) && isset($data["pay_cash"])) {
            return !empty($data["pay_cash"]) ? round($data["apply_cash"] - $data["pay_cash"], 2) : round($data["apply_cash"] - $data["true_cash"], 2);
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page, $where = [])
    {
        $data = $this->where($dis)->where(function ($query) use ($where){
            $query->whereOr($where);
        })->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getIdByNameV2($name)
    {
        $dis = [["b.coach_name", "like", "%" . $name . "%"], ["a.type", "in", [1, 2]]];
        $dis1 = [["c.agent_name", "like", "%" . $name . "%"], ["a.type", "=", 3]];
        $dis2 = [["d.nickName", "like", "%" . $name . "%"], ["a.type", "=", 4]];
        $dis3 = [["e.user_name", "like", "%" . $name . "%"], ["a.type", "=", 5]];
        $dis4 = [["f.user_name", "like", "%" . $name . "%"], ["a.type", "=", 6]];
        $dis5 = [["g.agent_name", "like", "%" . $name . "%"], ["a.type", "in", [7, 8, 9]]];
        $dis6 = [["h.user_name", "like", "%" . $name . "%"], ["a.type", "=", 10]];
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id", "left")->join("shequshop_school_admin c", "a.user_id = c.id", "left")->join("massage_service_user_list d", "a.user_id = d.id", "left")->join("massage_channel_list e", "a.coach_id = e.id", "left")->join("massage_salesman_list f", "a.coach_id = f.id", "left")->join("shequshop_school_admin g", "a.coach_id = g.id", "left")->join("massage_coach_broker_list h", "a.coach_id = h.id", "left")->where(function ($query) {
            $query->whereOr([$dis, $dis1, $dis2, $dis3, $dis4, $dis5, $dis6]);
        })->group("a.id")->column("a.id");
        return $data;
    }
    public function getIdByName($name)
    {
        $dis = [["b.coach_name", "like", "%" . $name . "%"], ["b.true_user_name", "like", "%" . $name . "%"], ["c.agent_name", "like", "%" . $name . "%"], ["d.nickName", "like", "%" . $name . "%"], ["e.user_name", "like", "%" . $name . "%"], ["e.true_user_name", "like", "%" . $name . "%"], ["f.user_name", "like", "%" . $name . "%"], ["f.true_user_name", "like", "%" . $name . "%"], ["g.agent_name", "like", "%" . $name . "%"], ["h.user_name", "like", "%" . $name . "%"], ["h.true_user_name", "like", "%" . $name . "%"], ["i.true_user_name", "like", "%" . $name . "%"]];
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id AND a.type in (1,2)", "left")->join("shequshop_school_admin c", "a.user_id = c.id AND a.type = 3", "left")->join("massage_service_user_list d", "a.user_id = d.id AND a.type = 4", "left")->join("massage_channel_list e", "a.coach_id = e.id AND a.type = 5", "left")->join("massage_salesman_list f", "a.coach_id = f.id AND a.type = 6", "left")->join("shequshop_school_admin g", "a.coach_id = g.id AND a.type in (7,8,9)", "left")->join("massage_coach_broker_list h", "a.coach_id = h.id AND a.type = 10", "left")->join("massage_distribution_list i", "a.user_id = i.user_id AND i.status in (2,3) AND a.type = 4", "left")->where(function ($query) use ($dis) {
            $query->whereOr($dis);
        })->group("a.id")->column("a.id");
        return $data;
    }
    public function datePrice($date, $uniacid, $cap_id, $end_time = "", $type = 1)
    {
        $end_time = !empty($end_time) ? $end_time : $date + 86399;
        $dis = [];
        $dis[] = ["status", "=", 2];
        $dis[] = ["create_time", "between", $date . "," . $end_time];
        $dis[] = ["uniacid", "=", $uniacid];
        if(!empty($cap_id)) {
            $dis[] = ["cap_id", "=", $cap_id];
        }
        if($type == 1) {
            $price = $this->where($dis)->sum("true_cash");
            return round($price, 2);
        }
        $count = $this->where($dis)->count();
        return $count;
    }
    public function adminList($dis, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id", "left")->where($dis)->field("a.*,b.coach_name")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function capCash($cap_id, $status = 2, $type = 0)
    {
        $dis = ["coach_id" => $cap_id, "status" => $status];
        if(!empty($type)) {
            $dis["type"] = $type;
        }
        $price = $this->where($dis)->sum("total_price");
        return round($price, 2);
    }
    public function userCash($user_id, $status = 2, $time = 0)
    {
        $dis = ["user_id" => $user_id, "type" => 4];
        if(!empty($status)) {
            $dis["status"] = $status;
        }
        $where = [];
        if(!empty($time)) {
            $where[] = ["create_time", ">", $time];
        }
        $price = $this->where($dis)->where($where)->sum("total_price");
        return round($price, 2);
    }
    public function capCashCount($cap_id, $status = 2)
    {
        $dis = ["cap_id" => $cap_id, "status" => $status];
        $count = $this->where($dis)->count();
        return $count;
    }
    public function adminCash($admin_id, $status = 2)
    {
        $where = [["user_id", "=", $admin_id], ["type", "=", 3]];
        $where1 = [["coach_id", "=", $admin_id], ["type", "in", [7, 8, 9]]];
        $dis = ["status" => $status];
        $price = $this->where(function ($query) use ($where, $where1) {
            $query->whereOr([$where, $where1]);
        })->where($dis)->sum("total_price");
        return round($price, 2);
    }
    public function wxCheck($uniacid, $payConfig, $id = [])
    {
        $dis = ["online" => 1, "status" => 4];
        $where = [];
        if(!empty($id)) {
            $where[] = ["id", "in", $id];
        }
        $data = $this->where($dis)->where($where)->limit(10)->select()->toArray();
        $wx_pay = new \longbingcore\wxcore\WxPay($uniacid);
        if(!empty($data)) {
            foreach ($data as $v) {
                $res = $wx_pay->getMchPayRecord($v["payment_no"], $payConfig);
                if(isset($res["detail_status"])) {
                    if($res["detail_status"] == "SUCCESS") {
                        $this->dataUpdate(["id" => $v["id"]], ["status" => 2]);
                    }
                    if($res["detail_status"] == "FAIL") {
                        $this->dataUpdate(["id" => $v["id"]], ["status" => 5, "failure_reason" => $res["fail_reason"]]);
                    }
                }
                if(isset($res["code"]) && $res["code"] == "NOT_FOUND") {
                    $this->dataUpdate(["id" => $v["id"]], ["status" => 5, "failure_reason" => "NOT_FOUND"]);
                }
            }
        }
        return true;
    }
    public function adapayWalletCheck($uniacid)
    {
        $dis = ["online" => 4, "status" => 4];
        $where = [];
        if(!empty($id)) {
            $where[] = ["id", "in", $id];
        }
        $data = $this->where($dis)->where($where)->limit(20)->select()->toArray();
        $adapay = new \longbingcore\wxcore\Adapay($uniacid);
        $callbak_model = new \app\adapay\model\Callback();
        if(!empty($data)) {
            foreach ($data as $v) {
                $res = $adapay->drawCashInfo($v["adapay_code"]);
                if(isset($res["status"]) && $res["status"] == "succeeded") {
                    $this->dataUpdate(["id" => $v["id"]], ["status" => 2]);
                    $insert = ["uniacid" => $uniacid, "adapay_id" => $v["payment_no"], "status" => $res["status"], "type" => "wallet", "text" => serialize($res)];
                    $callbak_model->dataAdd($insert);
                }
            }
        }
        return true;
    }
    public function agentCheckAuth($admin_id, $data, $auth)
    {
        if($data["type"] == 3) {
            $wallet_check_auth = $data["user_id"] == $admin_id ? 0 : $auth;
        } elseif(in_array($data["type"], [8, 9])) {
            $wallet_check_auth = $data["coach_id"] == $admin_id ? 0 : $auth;
        } else {
            $wallet_check_auth = $auth;
        }
        return $wallet_check_auth;
    }
    public function getWalletObjInfo($type)
    {
        switch ($type) {
            case 1:
                $data["model"] = new Coach();
                $data["title"] = "coach_name";
                $data["admin_id"] = "admin_id";
                $data["true_user_name"] = "true_user_name";
                break;
            case 2:
                $data["model"] = new Coach();
                $data["title"] = "coach_name";
                $data["admin_id"] = "admin_id";
                $data["true_user_name"] = "true_user_name";
                break;
            case 3:
                $data["model"] = new Admin();
                $data["title"] = "agent_name";
                $data["admin_id"] = "admin_id";
                $data["user_id"] = "user_id";
                break;
            case 4:
                $data["model"] = new User();
                $data["title"] = "nickName";
                $data["user_id"] = "user_id";
                break;
            case 5:
                $data["model"] = new ChannelList();
                $data["title"] = "user_name";
                $data["admin_id"] = "admin_id";
                $data["true_user_name"] = "true_user_name";
                break;
            case 6:
                $data["model"] = new Salesman();
                $data["title"] = "user_name";
                $data["admin_id"] = "admin_id";
                $data["true_user_name"] = "true_user_name";
                break;
            case 7:
                $data["model"] = new Admin();
                $data["title"] = "agent_name";
                $data["admin_id"] = "admin_id";
                break;
            case 8:
                $data["model"] = new Admin();
                $data["title"] = "agent_name";
                $data["admin_id"] = "admin_id";
                break;
            case 9:
                $data["model"] = new Admin();
                $data["title"] = "agent_name";
                $data["admin_id"] = "admin_id";
                break;
            case 10:
                $data["model"] = new \app\coachbroker\model\CoachBroker();
                $data["title"] = "user_name";
                $data["true_user_name"] = "true_user_name";
                break;
            case 11:
                $data["model"] = new User();
                $data["title"] = "nickName";
                $data["user_id"] = "user_id";
                break;
            default:

        }
        return $data;
    }
    public function agentTransferCheck($type)
    {
        $arr = ["offline_transfer_auth", "wechat_transfer_auth", "alipay_transfer_auth", "4" => "bank_transfer_auth"];
        return array_key_exists($type, $arr) ? $arr[$type] : "offline_transfer_auth";
    }
}

?>